package day14;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

//c.设计一个用户对象包含(手机号码(String),姓名(String),
//密码(String),套餐对象,消费金额(double),当前余额(double),
// 真实通话时长(int),真实短信条数(int),真实使用流量(int))
// 要求对类进行封装
public class User implements Serializable {
    private String phone;
    private String name;
    private String password;
    private Package pack;//套餐父类类型
    private double consumerMoney;
    private double balance;
    private int realTall;
    private int realCount;
    private int realFlow;
    public User(String phone, String name, String password, Package pack, double consumerMoney, double balance) {
        this.phone = phone;
        this.name = name;
        this.password = password;
        this.pack = pack;
        this.consumerMoney = consumerMoney;
        this.balance = balance;
    }
    public void print(){
        DecimalFormat df=new DecimalFormat("#.##");
        System.out.println("注册成功,您的手机号:"+phone+",姓名:"+name);
        System.out.println("消费金额:"+consumerMoney+",当前余额:"+df.format(balance));
        pack.print();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Package getPack() {
        return pack;
    }

    public void setPack(Package pack) {
        this.pack = pack;
    }

    public double getConsumerMoney() {
        return consumerMoney;
    }

    public void setConsumerMoney(double consumerMoney) {
        this.consumerMoney = consumerMoney;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public int getRealTall() {
        return realTall;
    }

    public void setRealTall(int realTall) {
        this.realTall = realTall;
    }

    public int getRealCount() {
        return realCount;
    }

    public void setRealCount(int realCount) {
        this.realCount = realCount;
    }

    public int getRealFlow() {
        return realFlow;
    }

    public void setRealFlow(int realFlow) {
        this.realFlow = realFlow;
    }
}
//a.定义三个接口 通话接口(提供通话的方法)  短信接口(提供短信方法)
//上网接口(提供上网方法)
interface CallService{//通话接口
    public void call();
}
interface SendService{//短信接口
    public void send();
}
interface NetService{//上网接口
    public void net();
}
//b.设计套餐对象的抽现象类具有(具有套餐价格属性, 打印信息方法)
//   设计三个实现类 来继承抽象类
//   话唠套餐类(可以通话和发短信 所以要实现通话和短信接口)
//   超人套餐类(可以通话和发短信和使用流量 所以要实现通话 短信和上网接口)
//   网虫套餐类(只能上网 只需要实现上网接口)
abstract class Package implements Serializable{ //套餐
    double price;
    public abstract void print();
}
class TallPackage extends Package{//话痨
    public TallPackage(double price){
        super.price=price;
    }
    public void print() {
        System.out.println("我是话痨套餐,价格:"+price);
    }
}
class SuperPackage extends Package{//超人
    public SuperPackage(double price){
        super.price=price;
    }
    public void print() {
        System.out.println("我是超人套餐,价格:"+price);
    }
}
class NetPackage extends Package{ //网虫
    public NetPackage(double price){
        super.price=price;
    }
    public void print() {
        System.out.println("我是网虫套餐,价格:"+price);
    }
}
//d.实现注册功能 要求:
//选择卡号（提供以“139”开头的9个随机卡号,每行3个）
//选择套餐（话唠套餐、网虫套餐、超人套餐） 输入用户名、密码信息
//输入预存话费金额
//成功保存信息并提示 显示(卡号、用户名、当前余额、套餐信息)
class TestUser{
    //定义保存用户信息的集合
    static Map<String,User> map=new HashMap<>();
    static Scanner sc=new Scanner(System.in);
    static{//反序列化 第一次加载数据
        input();
    }
    public static void main(String[] args) {
        one();
    }
    public static void one(){//一级菜单
        while(true){
            System.out.println("XXXX用户系统");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.println("请输入编号");
            int n=sc.nextInt();
            switch (n){
                case 1: login();break;
                case 2: reg();break;
                case 3: return;
            }
        }
    }
    public static void login(){
        System.out.println("登录功能:先遍历下前面加载好的map集合");
        for(String key:map.keySet()){
            User value=map.get(key);
            value.print();
        }
    }
    public static void reg(){
        String[] phones=randPhone();
        System.out.println("***请选择你喜欢的号码***");
        for(int i=0;i<phones.length;i++){
            System.out.print(i+1+"."+phones[i]+" ");
            if((i+1)%3==0) System.out.println();
        }
        System.out.println("请输入编号1-9");
        int n=sc.nextInt();
        String phone=phones[n-1];
        System.out.println("请输入你要选择的套餐:");
        System.out.println("1.话痨套餐 2.超人套餐 3.网虫套餐");
        int m=sc.nextInt();
        Package p=null;
        switch (m){
            case 1:p=new TallPackage(19.9); break;
            case 2:p=new SuperPackage(29.9); break;
            case 3:p=new NetPackage(9.9); break;
        }
        Package page=p;
        System.out.println("请输入用户姓名");
        String name=sc.next();
        System.out.println("请输入密码:");
        String password=sc.next();
        System.out.println("请输入预存的话费");
        double money=0;
        while(true) {
            money = sc.nextDouble();
            if(money<p.price){
                System.out.println("预存的话费不足以支付这个套餐,请重新充值");
            }else{
                break;
            }
        }
        User user=new User(phone,name,password,
                p,p.price,money-p.price);
        user.print();
        map.put(phone,user);
        //序列化
        output();
    }
    public static void input(){//反序列化
        try(FileInputStream fis=new FileInputStream("d://io//map.XX");
            ObjectInputStream ois=new ObjectInputStream(fis)){
            map=(Map<String,User>)ois.readObject();
            System.out.println("用户信息获取成功");
        }catch (FileNotFoundException e){
            System.out.println("第一次使用");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void output(){//序列化
        File f=new File("d://io//map.XX");
        if(!f.getParentFile().exists()){
            f.getParentFile().mkdirs();
        }
        try(FileOutputStream fos=new FileOutputStream(f);
        ObjectOutputStream oos=new ObjectOutputStream(fos)){
            oos.writeObject(map);
            System.out.println("用户信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static String[] randPhone(){
        Random r=new Random();
        String[] phones=new String[9];
        for(int i=0;i<phones.length;i++){
            StringBuilder sb=new StringBuilder("139");
            for(int j=0;j<8;j++){
                sb.append(r.nextInt(10));
            }
            phones[i]=sb.toString();
        }
        return phones;
    }
}